package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.repository.IStrategyCommentRepository;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private IStrategyCommentRepository strategyCommentRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveOrUpdate(StrategyComment strategyComment) {
        //保存评论
        if (strategyComment.getId() == null) {
            strategyComment.setCreateTime(new Date());
            strategyCommentRepository.save(strategyComment);
        } else {
            //更新点赞数
            Query query = new Query();
            query.addCriteria(Criteria.where("id").is(strategyComment.getId()));
            mongoTemplate.updateFirst(query, Update.update("thumbupnum", strategyComment.getThumbupnum())
                    .set("thumbuplist",strategyComment.getThumbuplist()), StrategyComment.class);
        }
    }

    @Override
    public Page query(StrategyCommentQuery qo) {
        Query query = new Query();

        if (qo.getDetailId() != -1) {
            query.addCriteria(Criteria.where("detailId").is(qo.getDetailId()));
        }

        //先查总数
        long count = mongoTemplate.count(query, StrategyComment.class);
        if (count == 0) {
            return Page.empty();
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.by(Sort.Direction.DESC, "createTime"));
        query.with(pageable);
        //查询分页的数据
        List<StrategyComment> list = mongoTemplate.find(query, StrategyComment.class);
        return new PageImpl(list,pageable,count);
    }

    @Override
    public StrategyComment commentThumbUp(String toid, Long fromid) {
        //查询当前id的评论中的点赞集合
        Optional<StrategyComment> op = strategyCommentRepository.findById(toid);
        StrategyComment comment = op.get();
            List<Long> list = comment.getThumbuplist();
            int thumbupnum = comment.getThumbupnum();
            //取消点赞
            if (list.contains(fromid)) {
                comment.setThumbupnum(thumbupnum-1);
                list.remove(fromid);
            }else {
                //点赞
                comment.setThumbupnum(thumbupnum+1);
                list.add(fromid);
            }
        return comment;
    }
}
